﻿using HBase;
using hCommand;
using Net.http;
using Persistence;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text.RegularExpressions;

namespace WebApi.Controller {
    [RoutePrefix("pda")]
    public class PdaController : BaseApiController {


        ICoreStockServer coreStockServer = WMSDalFactray.getDal<ICoreStockServer>();
        ICSDServer csdServer = WMSDalFactray.getDal<ICSDServer>();
        //原料分拣出库界面显示
        [HttpGet, Route("getStock/{boxCode}")]
        public BaseResult getStock(String boxCode) {
            CoreStock list = coreStockServer.getCoreStockByCode(boxCode);
            if (list == null) {
                return BaseResult.Error("托盘条码不存在");
            }
            StockDto dto = new StockDto();
            dto.main = list;
            dto.detList = csdServer.getDetail(dto.main.id, true).OrderBy(x => x.status).ToList();
             
            return new BaseResult(dto);
        }


        [HttpGet, Route("getItem/{name}")]
        public BaseResult getitem(String name) {
            List<CoreItem> list = MyCache.getList<CoreItem>();
            List<CoreItem> res = new List<CoreItem>();
            foreach (CoreItem ci in list) {
                if ((ci.code + ci.name + ci.gg).IndexOf(name) != -1) {
                    res.Add(ci);
                    if (res.Count == 10) {
                        break;

                    }
                }

            }


            return new BaseResult(res);
        }
        [HttpGet, Route("getUnits")]
        public BaseResult getUnits() {
            List<DictRes> dicts = MyCache.getList<DictRes>();
            List<DictRes> res = new List<DictRes>();
            foreach (DictRes dict in dicts) {
                if (dict.dType == "units" && dict.type == 1) {
                    res.Add(dict);
                }


            }

            return new BaseResult(res);
        }


        //原料分拣出库界面显示
        [HttpPost, Route("outDetail")]
        public BaseResult outDetail([FromBody] stockInfoDto dto) {
            List<Pair> list = new List<Pair>();

            return new BaseResult(list);
        }

        //托盘出库
        [HttpPost, Route("outStockByBoxCode"), ControlName("托盘出库")]
        public BaseResult outStockByBoxCode([FromBody] stockInfoDto dto) {
            string[] sArray = Regex.Split(dto.boxCode, ",", RegexOptions.IgnoreCase);
            for (int i = 0; i < sArray.Length - 1; i++) {
                CoreStock coreStock = null;
                if (coreStock == null) {
                    return BaseResult.Error("托盘条码不存在");
                }
                //
                var coreStockDetailList = coreStockServer.GetAll<CoreStockDet>("and pId=@stockId", new { stockId = coreStock.id });
                if ( coreStockDetailList.Count != 0) {
                    return BaseResult.Error("出库明细不存在");
                }
                coreStockServer.UpdateStatusByStockId(coreStock.id, StockStatus.PDA出库删除);
                
            }
            return BaseResult.Ok("ok");
        }

        //箱条码出库
        [HttpPost, Route("outStockByBarCode"), ControlName("箱条码出库")]
        public BaseResult outStockByBarCode([FromBody] stockInfoDto dto) {

            return BaseResult.Ok("ok");
        }



        public BaseResult SaveStock(StockDto model) {

            CoreStock stock = model.main;
            if (stock == null) {

                return BaseResult.Error("托盘信息异常");
            }

            if (stock.id == 0) {
                stock.status = (int)StockStatus.库外;
                stock.createTime = DateTime.Now;
                stock.changeTime = stock.createTime;
            }
            coreStockServer.saveOne2Many(model);

            return BaseResult.Ok("修改完成");


        }
        //采购入库
        [HttpPost, Route("saveStock"), ControlName("保存托盘")]
        public BaseResult saveStock([FromBody] StockDto dto) {

            CoreStock cs = coreStockServer.getCoreStockByCode(dto.main.boxCode);
            if (cs != null) {
                if (cs.status != (int)StockStatus.库外) {
                    return new BaseResult("条码不在库外");
                }
            } else {
                cs = new CoreStock();

                cs.boxCode = dto.main.boxCode;
                cs.status = (int)StockStatus.库外;
               
            }
            dto.main = cs;
            for (int i = 0; i < dto.detList.Count; i++) {
                var det = dto.detList[i];
                if (det.id != 0) {
                    var now = coreStockServer.getById<CoreStockDet>(det.id);
                    now.countDb = det.countDb;
                    dto.detList[i] = now;
                }
            }
            coreStockServer.saveOne2Many(dto);

            return new BaseResult("ok");

        }
        //产成品入库
        [HttpPost, Route("saveStocks"), ControlName("产成品入库")]
        public BaseResult saveStocks([FromBody] stockInfosDto dto) {
            CoreStock list = coreStockServer.getCoreStockByCode(dto.boxCode);
            CoreStock cs = new CoreStock();
            if (list == null) {

                cs.boxCode = dto.boxCode;
                cs.status = 1;
                cs.changeTime = DateTime.Now;


              

                long cid = cs.id;

            } else {
                cs = list;
            }

            List<CoreStockDet> cpList = csdServer.GetListByStockId(cs.id);
            CoreStockDet csd = new CoreStockDet();
            long stockDetailId = 0;
            if (cpList.Count < 1) {

                csd.pid = list.id;
                csd.countDb = 1;
                csd.itemId = 0;


                csd.status = 1;

                stockDetailId = csd.id;
            } else {
                stockDetailId = cpList[0].id;
            }

            return new BaseResult("ok");
        }
        //成品分拣
        [HttpPost, Route("saveFjStocks"), ControlName("成品分拣")]
        public BaseResult saveFjStocks([FromBody] stockInfosDto dto) {
            CoreStock list = coreStockServer.getCoreStockByCode(dto.boxCode);
            if (list == null) {
                return new BaseResult("不存在该托盘!");
            }
            return new BaseResult("ok");
        }
        //原料分拣
        [HttpPost, Route("saveYlStock"), ControlName("成品分拣")]
        public BaseResult saveYlStock([FromBody] stockInfoDto dto) {

            return BaseResult.Ok("成功");
        }
        //成品盘库
        [HttpPost, Route("savePkStocks"), ControlName("成品分拣")]
        public BaseResult savePkStocks([FromBody] stockInfosDto dto) {

            CoreStock list = coreStockServer.getCoreStockByCode(dto.boxCode);
            if (list == null) {
                return new BaseResult("不存在该托盘!");
            }
            //一个托盘只能用一个明细
            var listDetail = csdServer.GetListByStockId(list.id);
            if (listDetail.Count > 1) {
                return new BaseResult("数据异常!");
            }
            return new BaseResult("ok");
        }
        //原料盘库
        [HttpPost, Route("saveYpStock"), ControlName("成品分拣")]
        public BaseResult saveYpStock([FromBody] stockInfoDto dto) {

            return new BaseResult("ok");
        }

        [HttpGet, Route("bandDetPingku/{id}/{pid}"), ControlName("平库入库")]
        public BaseResult bandDetPingku(long id, long pid) {

            TransRes r = WMSTransactionFacade.doCsd2CsEngine(id, pid,true);
            return new BaseResult(r.msg);


        }

        [HttpGet, Route("pinkuIn/{boxCode}/{des}/{type}"), ControlName("更改位置")]
        public BaseResult pinkuIn(string boxCode, int des, int type) {
            if (des > 2000) {
                return new BaseResult("目标货位过大" + des);
            }
            if (string.IsNullOrWhiteSpace(boxCode)) {
                return new BaseResult("请输入托盘号");
            }
            String r = coreStockServer.pingkuChangeLoc(boxCode, des);
            return new BaseResult(r);
          
        }

    }
    public class stockInfoDto {
        public long detailId;
        public int countInput;
        public string boxCode;
        public int isCouyan;
        public int isZhentuo;
        public string barCode;
    }
    public class stockInfosDto {
        public string boxCode;
        public string barCode;
        public int isZhentuo;
    }
}
